package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

    @Autowired
    private IAgencyCertificationAuditService agencyCertificationAuditService;

    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }


    /**
     * 机构认证提交
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submit(AgencyCertificationAuditAddReqDTO dto) {
        //判断参数
        if(ObjectUtil.isEmpty(dto)){
            throw new BadRequestException("参数错误");
        }
        //创建存储对象
        AgencyCertification agencyCertification = BeanUtils.copyBean(dto, AgencyCertification.class);
        agencyCertification.setId(UserContext.currentUserId());
        agencyCertification.setCertificationStatus(1);
        //存入agency表
        boolean save = this.saveOrUpdate(agencyCertification);
        if (!save){
            throw new BadRequestException("添加机构认证信息失败");
        }
        //同时存入audit表
        //构造audit对象
        AgencyCertificationAudit agencyCertificationAudit = BeanUtils.copyBean(dto, AgencyCertificationAudit.class);
        agencyCertificationAudit.setServeProviderId(UserContext.currentUserId());
        //状态为未审核
        agencyCertificationAudit.setAuditStatus(0);
        //存入audit
        boolean save1 = agencyCertificationAuditService.save(agencyCertificationAudit);
        if (!save1){
            throw new BadRequestException("添加机构认证审核信息失败");
        }

    }

    @Override
    public Map getRejectReason() {
        //查询审核状态为审核
        //状态为审核失败 3
        //根据审核时间排序
        List<AgencyCertificationAudit> list = agencyCertificationAuditService.lambdaQuery()
                .eq(AgencyCertificationAudit::getAuditStatus, 1)
                .eq(AgencyCertificationAudit::getCertificationStatus, 3)
                //机构id
                .eq(AgencyCertificationAudit::getServeProviderId, UserContext.currentUserId())
                .orderByDesc(AgencyCertificationAudit::getAuditTime)
                .list();
        Map<String, String> stringStringMap = new HashMap<>();
        //只要最新的一条
        if (ObjectUtils.isNotEmpty(list)) {
            stringStringMap.put("rejectReason", list.get(0).getRejectReason());

        }
        return stringStringMap;
    }

}
